<template>
  <div class="a1">
    <div class="login-page">
      <header class="header">
        <span class="back-arrow" @click="$router.push('/OperatorLogin')">←</span>
        <span class="help">帮助</span>
      </header>
      <div class="login-form">
        <h1 class="title">登录后即可展示自己</h1>
        <p class="agreement">
          登录则表明同意 <a href="#">用户协议</a> 和 <a href="#">隐私协议</a>
        </p>
        <div class="input-group">
          <select class="country-code">
            <option value="+86">+86</option>
          </select>
          <input type="text" class="phone-input" placeholder="请输入手机号" v-model="phoneNumber" @input="validatePhoneNumber" />
          <p v-if="isPhoneNumberValid" class="valid-message">手机号有效</p>
          <p v-if="!isPhoneNumberValid && phoneNumber !== ''" class="invalid-message">手机号无效，请输入正确的手机号</p>
        </div>
        <small class="note">未注册的手机号验证通过后将自动注册</small>
        <button :disabled="isButtonDisabled" class="sms-button" @click="sendSmsCode">{{ countdown > 0 ? `${countdown}秒后重新获取` : '获取短信验证码' }}</button>
        <div class="other-login-options">
          <button class="other-button" @click="$router.push('/PasswordLogin')">密码登录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      user:{
        phone:''
      },
      phoneNumber: '',
      isPhoneNumberValid: null,// 验证结果，null表示未验证，true表示验证通过，false表示验证失败
      isButtonDisabled: false, // 控制按钮是否禁用
      countdown: 0, // 倒计时
    };
  },
  methods: {
    ddlogin(){

    },
    validatePhoneNumber() {
      const phoneRegex = /^1[3-9]\d{9}$/;
      this.isPhoneNumberValid = phoneRegex.test(this.phoneNumber);
    },
     sendSmsCode() {
      if (this.isPhoneNumberValid) {
        this.user.phone = this.phoneNumber;
        this.axios.post("http://localhost:9000/user/verification",this.user).then(res => {
          this.$router.push({ name: '/verification', params: { phoneNumber: this.phoneNumber } });
        })
      }
    }
  },
   watch: {
    phoneNumber(newVal) {
      if (newVal === '') {
        this.isPhoneNumberValid = null;
      } else {
        this.validatePhoneNumber();
      }
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f9f9f9;
}

.login-page {
  width: 100%;
  height: 100%;
  max-width: 400px; /* 设定最大宽度 */
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  background-color: #fff;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.back-arrow {
  font-size: 24px;
  cursor: pointer;
}

.help {
  font-size: 14px;
}

.title {
  font-size: 24px;
  font-weight: bold;
  text-align: left;
  margin-bottom: 20px;
}

.agreement {
  text-align: center;
  margin: -15px 0 ;
}

.input-group {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
  margin-top: 70px;
}

.country-code{
  width: 50px;
  height: 40px;
  border: 1px solid #ccc;
  border-radius: 5px;
}
.phone-input {
  flex: 1;
  height: 40px;
  margin: 5px;
  padding: 0 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

.sms-button {
  width: 100%;
  padding: 10px;
  margin: 10px 0;
  background-color: #ccc;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.sms-button:disabled {
  background-color: #eee;
}

.other-login-options {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
}

.other-button {
  flex: 1;
  padding: 10px;
  margin: 5px;
  background-color: #f5f5f5;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.note {
  text-align: left;
  font-size: 12px;
  color: #888;
  margin-bottom: 15px;
}
</style>
